﻿<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加商品</title>
    <link rel="stylesheet" href="<%=basePath %>web/css/from.css">
    <link rel="stylesheet" href="<%=basePath %>web/themes/default/easyui.css">
    <link rel="stylesheet" href="<%=basePath %>web/themes/icon.css">
    <script src="<%=basePath %>web/js/jquery.min.js"></script>
    <script src="<%=basePath %>web/js/jquery.easyui.min.js"></script>
    <script src="<%=basePath %>web/js/easyui-lang-zh_CN.js"></script>
    <script>
         //添加供应商
        function loadSupplier() {
            $("#list").dialog({
                title: '供应商列表',
                width: 905,
                height: 400,
                closed: false,
                cache: false,
                modal: true
             });

        }
   //添加信息
  $(function(){
   var  code=$("#code").val();
   //alert(code)
        $("#generatingParts").datagrid({  
                       
                checkOnSelect:true,
                rownumbers:true,
                url:'/kxmanage/IBC/PartBycode?code='+code,                   
                columns:[[    
                    {field:'checkStatus',title:'状态',width:150,hidden:true},                 
                    {field:'code',title:'配件单号',width:150},
                    {field:'partName',title:'配件名称',width:173},
                    {field:'partBrand',title:'配件品牌',width:110},
                    {field:'partNo',title:'配件型号',width:100},
                    {field:'amount',title:'数量',width:80,
                     formatter:function(value,row,index){
                       if(row.sellPrice==null){
                    		return value;
                    	}
                    	if(row.checkStatus==2){
                            return  value;            
                        }else{
                           return '<input id="amount1" name="amount" onchange="change_amount_sellPrice('+index+',this)" type="number" value="'+value+'" style="width:50px;"/>';
                          
                        }
                    }
                    
                    },
                    {field:'sellPrice',title:'单价(￥)',width:80,
                         formatter:function(value,row,index){
                            if(row.sellPrice==null){
                    		return null;
                    	    }
                    	    if(row.checkStatus==2){
                            return value 
                            }else{
                             return '<input    id="sellPrice1"name="sellPrice" onchange="change_amount_sellPrice('+index+',this)" type="number" value="'+value+'" style="width:50px;"/>';
                            }
                         }
                    },
                    {field:'price',title:'金额(￥)',width:80},
                    {field:'memo',title:'备注',width:180,
                    formatter:function(value,row,index){
                         if(row.sellPrice==null){
                    		return null;
                         }
                    	  if(value==null||value==''){
                    		value='';
                    	  }
                    	  if(row.checkStatus==2){
                    	  
                            return value
                               
                        }else{
                      return '<input   id="partmemo1" class="partmemo" type="text" value="'+value+'" />';
                        }
                    }
                  },
                   	{field:'opt',title:'操作',width:92,
                   	    formatter:function (value,row,index) {
                            if(row.sellPrice==null){
    	                		return null;
	                    	}
	                    	 if(row.checkStatus==2){
                            var context="<a id=\"del1\" href=\"javascript:del('"+index+"')\">删除</a>&nbsp;&nbsp;"
                            return context;
                            }else{
                            var context="<a id=\"del1\"  href=\"javascript:del('"+index+"')\">删除</a>&nbsp;&nbsp;"
                             return context;
                            }
                          }
                        }
                	]],
                	 
                	
                	onClickRow:function(index,data){
                	$(this).datagrid('unselectRow',index);
                },
//添加合计                  
                    onLoadSuccess:function(){
               		oldRow=$("#generatingParts").datagrid('getRows');
               		
               		 if(oldRow[0].checkStatus=='2'){
               		   // $("#addpeada").arttr()
               		     //$("#addpeada").attr("readonly":"readonly");
               		     $("#addpeada").attr("disabled", true); 
               		      $("#update").attr("disabled", true);
               		     
               		 };
               		if(oldRow[oldRow.length-1].sellPrice!=null){
                	$("#generatingParts").datagrid('appendRow',{
                	'partBrand':"合计",
                	'amount':total('amount'),
                	'price':total('price')
                	});
               		}
                  }
        	});  

 
 }); 
 
 // 
        $(function() {
        var oldRow2=$('#generatingParts').datagrid('getRows');
     
        });
 
      
  //合计方法
      function total(col){
        	var rows=$("#generatingParts").datagrid('getRows');
        	var total=0;
        	for(var i=0;i<rows.length;i++){
        		total+=parseFloat(rows[i][col]);
        	}
        	return total;
        } 
 //改变合计
 
         function change_amount_sellPrice(input_index,input){
                                            
         	var amount=$(input).parent().parent().parent().find('[name="amount"]').val();
         	var avg=$(input).parent().parent().parent().find('[name="sellPrice"]').val();
         	var price=parseFloat(amount)*parseFloat(avg);
         	$('#generatingParts').datagrid('updateRow',{
				index: input_index,
				row: {
					amount: amount,
					sellPrice:avg,
					price: price
				}
			});
			var oldRow=$('#generatingParts').datagrid('getRows');
			
			$('#generatingParts').datagrid('deleteRow',oldRow.length-1); 
			$("#generatingParts").datagrid('appendRow',{
                	  'partBrand':"合计",
                	  'amount':total('amount'),
                	  'price':total('price')
           	});
         } 
 //绑定子类数据
            function bindValue(data){
        	var name=data[0];
        	var linkman=data[1];
        	var phone=data[2];
        	var fax=data[3];
        	var address=data[4];
        	$('#supplierName').textbox('setValue',name);	
        	$('#linkman').textbox('setValue',linkman);	
        	$('#phone').textbox('setValue',phone);	
        	$('#fax').textbox('setValue',fax);	
        }
        //销毁
         function closeSon(){
        	$("#list").dialog("close");
	 
        }
    //添加配件
    function loadPart() {
            $("#partList").dialog({
                title: '选择配件',
                width: 1000,
                height: 400,
                closed: false,
                cache: false,
                modal: true,
           })
        }

  //父类的addpart    
        function closePart() {
            $("#partList").dialog("close");
        };   
        
     
            //用于将子页面的数据绑定到相关元素上
        function addpart(row){
            var flag=true;
            var x=true;
            var oldRow=$('#generatingParts').datagrid('getRows');
            
            for(var i=0;i<row.length;i++){
               if(oldRow!=null&&oldRow!=''){
                 for(var j=0;j<oldRow.length;j++){
                   if(oldRow[j]['code']==row[i]['code']){
         		       alert("不能添加重复配件");
         		       flag=false;
         		       break;
         		   }
                 } 
                 if(flag==true){
                 }else{
                 	break;
                 }
                }else{
                	addToPage(row[i]);
                	x=false;
                }
             }
             if(flag==true){
             	if(x==true){
                 	for(var i=0;i<row.length;i++){
			         $('#generatingParts').datagrid('deleteRow',oldRow.length-1); 
                 	 addToPage(row[i]);
					}
                }else{
                 	for(var i=1;i<row.length;i++){
			         $('#generatingParts').datagrid('deleteRow',oldRow.length-1); 
                 	 addToPage(row[i]);
					}	
                }
              }
	              closePart();
         }

        
        function addToPage(row){
           $('#generatingParts').datagrid('appendRow',{
				code: row.code,
				partName: row.partName,
				partBrand: row.partBrand,
				partNo: row.partNo,
				amount: 1,
				sellPrice: row.sellPrice,
				price: row.sellPrice,
				memo: row.memo,
				opt: row.opt
		   });		 
		   $("#generatingParts").datagrid('appendRow',{
                	  'partBrand':"合计",
                	  'amount':total('amount'),
                	  'price':total('price')
          });
		     
        } 
 //保存修改的配置
  $(function(){
       var ms=[];
      $("#update").click(function(){
           
	         $(".partmemo").each(function(){
	             ms.push($(this).val());
	          })          
     var form=$("#form").serializeArray();
	 var oldRow=$('#generatingParts').datagrid('getRows');
	 var data =JSON.stringify($('#generatingParts').datagrid('getData').rows);
     // alert(data)
       var mm=JSON.stringify(ms);  
      //  alert(mm)   
     if(oldRow==null||oldRow==''){
     
        alert("没有添加配件信息")
        	}else{
        	var money=parseFloat(oldRow[oldRow.length-1]['price']);
        	 //alert(parseFloat(oldRow[oldRow.length-1]));        	 
        	  $.ajax({
        		type:"post",
        		url:"/kxmanage/IBC/modiftyIquiry?data="+data+"&money="+money+"&mm="+mm,
        		data:form,
        		dataType:"json",
        		success:function(data){
        			if(data.result=="success"){
        				alert("修改成功"); 
        		   //修改完成刷新操作
        			parent.addTabs("产品列表","goods/model.jsp");
					closeTabs();        				
        			}else{
        				alert("修改错误");
        			}
        		}
        		
        		});
        	  
        }  
    }); 
 
 }) 
 
 //关闭当前页
    	   function closeTabs(){
			parent.closeTabs("询价单");
		  } 
 
 //删除操作
       function del(index){
            //判断是否删除
         var  code=$("#code").val();
        // alert(code)
         $.ajax({
                 type:"post",
        		 url:"/kxmanage/IBC/isdel?code="+code,
        		 dataType:"text",        
                 success:function(data){
                // alert("data=="+data)
                 if(data=="2"){                     
                           alert("该配件所在订单的已审核不能删除")
                   }else{
                     //未审核  
                     //删除当前行
         	     $('#generatingParts').datagrid('deleteRow',index);
         	    //拿到之前的数据并重新封装，刷新index
          	    var oldData=$('#generatingParts').datagrid('getData');
         	    $('#generatingParts').datagrid('loadData',oldData);
         	   //更新最后一行
         	   var oldRow=$('#generatingParts').datagrid('getRows');
         	   $('#generatingParts').datagrid('deleteRow',oldRow.length-1); 
			           $("#generatingParts").datagrid('appendRow',{
                	  'partBrand':"合计",
                	  'amount':total('amount'),
                	  'price':total('price')
                      	}); 
                 
                             }                 
                  }                
        })          
   }        
   //审核
         $(function(){
               $("#shenhe").click(function(){
 ////              

       var ms=[];
         $(".partmemo").each(function(){
	             ms.push($(this).val());
	          })          
     var form=$("#form").serializeArray();
	 var oldRow=$('#generatingParts').datagrid('getRows');
	 var data =JSON.stringify($('#generatingParts').datagrid('getData').rows);
     // alert(data)
       var mm=JSON.stringify(ms);  
      //  alert(mm)   
     if(oldRow==null||oldRow==''){
     
        alert("没有添加配件信息")
        	}else{
        	var money=parseFloat(oldRow[oldRow.length-1]['price']);
        	 //alert(parseFloat(oldRow[oldRow.length-1]));        	 
        	  $.ajax({
        		type:"post",
        		url:"/kxmanage/IBC/modiftyIquiry?data="+data+"&money="+money+"&mm="+mm,
        		data:form,
        		dataType:"json",
        		success:function(data){
        			if(data.result=="success"){
        			//审核时完成添加
        				                
                              
               var  code=$("#code").val();
               //alert(code);
               $.ajax({
        		 type:"post",
        		 url:"/kxmanage/IBC/shenhela?code="+code,
        		 dataType:"text",
        		 success:function(data){
        			if(data=="success"){
        				alert("审核成功"); 
        				 document.getElementById("del1").readOnly=true;
        				 document.getElementById("sellPrice1").readOnly=true;
        				 document.getElementById("partmemo1").readOnly=true;
        				 document.getElementById("amount1").readOnly=true;
        				 
        		   //审核前添加完成
        		   //   修改完成刷新操作
        			parent.addTabs("产品列表","goods/model.jsp");
					closeTabs();        				
        			}else{
        				alert("修改错误");
        			}
        		}
        		
         }); 
        				
        				 
        		      //修改完成刷新操作
        			parent.addTabs("产品列表","goods/model.jsp");
					closeTabs();        				
        			}else{
        				alert("修改错误");
        			}
        		}
        		
        		});
        	  
        }    
               
               
               
               
 // ////             
              
        		
     })     
})  

//撤销审核
$(function(){
               $("#chexiao").click(function(){
               var  code=$("#code").val();
              // alert(code);
            $.ajax({
        		 type:"post",
        		 url:"/kxmanage/IBC/chexiaola?code="+code,
        		 dataType:"text",
        		 success:function(data){
        			if(data=="success"){
        				alert("撤销成功"); 
        		   //修改完成刷新操作
        			parent.addTabs("产品列表","goods/model.jsp");
					closeTabs();        				
        			}else{
        				alert("撤销错误");
        			}
        		}
        		
         });               
        		
     })     
})  
//guan
 $(function(){
          $("#guanbixun2").click(function(){
          
           parent.addTabs("产品列表","goods/model.jsp");
			closeTabs();  
          
          
          })
 
 })
 
    </script>
    <style>
        body {
            font-size:12px;
            padding:10px
        }
        .l-table-edit {
            border:#84a0c4 1px solid
        }
        .l-table-edit-td {
            padding:4px;
            font-size:12px;
        }
        
    </style>


</head>
<body>

<form name="form"  id="form" action="" method="post">
<input type="hidden" id="outcode" value="${oB.code}">
    <table cellpadding="0"  cellspacing="0" class="l-table-edit" style="width:100%"  >
        <tr >
            <td width="17%"  align="right" class="l-table-edit-td">询价单号：</td>
            <td width="27%" align="left" class="l-table-edit-td"><input class="easyui-textbox" name="code" type="text" id="code" value="${oB.code}" size="30" maxlength="100" ltype="text" /></td>
            <td width="13%"  align="right" class="l-table-edit-td">询价日期：</td>
            <td width="43%" align="left" class="l-table-edit-td"><input class="easyui-datetimebox" name="inquiryTime"  id="addDate" value='<fmt:formatDate pattern="yyyy-MM-dd HH:mm:ss" value="${oB.inquiryTime }"/>'  style="width:168px"/></td>
        </tr>
        <tr>
            <td width="17%" align="right" class="l-table-edit-td">
                <a href="javascript:loadSupplier()">供应商名称</a>：
            </td>
            <td align="left" class="l-table-edit-td">
                <input class="easyui-textbox" name="supplierName" type="text" id="supplierName"  value="${oB.supplierName }"  maxlength="100" ltype="text"  style="width:239px"/>
            <td width="13%" align="right" class="l-table-edit-td">联系人员：</td>
            <td align="left" class="l-table-edit-td">
                <input class="easyui-textbox" name="linkman" type="text" id="linkman" value="${oB.linkman }" maxlength="30" ltype="text" /></td>
        </tr>
        <tr>
            <td width="17%" align="right" class="l-table-edit-td">电&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;话：</td>
            <td align="left" class="l-table-edit-td"><input class="easyui-textbox" name="phone"  type="text" id="phone" value="${oB.phone }" size="30" maxlength="50" ltype="text" /></td>
        </tr>
        <tr>
            <td width="17%" align="right" class="l-table-edit-td">传&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;真：</td>
            <td align="left" class="l-table-edit-td"><input class="easyui-textbox" type="text" id="fax" value="${coy.fax }" size="30" maxlength="50" ltype="text" /></td>
        </tr>
        <tr>
            <td width="17%" align="right" class="l-table-edit-td">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注：</td>
            <td align="left" class="l-table-edit-td"><input class="easyui-textbox" name="memo" type="text"  class="l-table-edit" id="memo" value="${oB.memo }" maxlength="50"  ltype="text" style="width:168px"/>
            <input type="hidden" value="1" name="payStatus">
            </td>
        </tr>
        
    </table>
</form>
<br>
<a href="javascript:loadPart()"><input id="addpeada" type="button" value="添加配件" style="width:60px"></a>
<input  type="button" id="update" value="保存" style="width:60px">
<input  type="button" id="shenhe" value="审核" style="width:60px">
<input  type="button" id="chexiao" value="撤销" style="width:60px">
<input  type="button" id="guanbixun2" value="关闭" style="width:60px">
<br/>
<div class="easyui-datagrid" id="generatingParts"></div>
<div id="absolute">
<!-- 供应商 -->
<div id="list"  style="display: none"  data-options="onClose:function(){closeSon()}">
    <iframe src='<%=basePath %>web/goods/company2.jsp' frameborder='0'style='width: 100%;height: 100%'></iframe>
</div>
</div>
<div id="part_List_parent">
<div id="partList"  style="display: none" >
    <iframe src='<%=basePath %>web/goods/addPart2.jsp' frameborder='0'style='width: 100%;height: 100%'></iframe>
</div>
</div>
</body>
</html>